package br.gendi.core;

import java.net.URL;

import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.NVPair;

public class Conectividade {


	private static final boolean proxy = false;
	private static final String host = "proxy.reconcavo";
	private static final int port = 3128;
	
	public static String cortarHTML(String html, String site) throws Exception {
		int inicio = html.indexOf("location.replace(") + 19;
		int fim = html.lastIndexOf("\")");
		return site + html.substring(inicio, fim);

	}

	public static String retornarHTML(String texto) throws Exception {
		HTTPResponse rsp = null;
		HTTPConnection con = null;
		String site = "http://www.cbs.dtu.dk/";

		con = new HTTPConnection(new URL("http://www.cbs.dtu.dk/services/NetMHC/"));
		if (proxy)
			con.setCurrentProxy(host, port);

		NVPair[] post = { 
				new NVPair("configfile", "/usr/opt/www/pub/CBS/services/NetMHC-3.0/NetMHC.cf"), 
				new NVPair("SEQPASTE", texto),
				new NVPair("peplen", "9"),
				new NVPair("alleles", "A0101")
		};

		rsp = con.Post("/cgi-bin/webface", post);

		String retorno = rsp.getText();
		String ret2 = cortarHTML(retorno, site);
		//System.out.println(ret2);
		//System.out.println(retorno);


		return ret2;
	}

	public static String retornarHTMLEpijen(String texto) throws Exception {
		HTTPResponse rsp = null;
		HTTPConnection con = null;

		con = new HTTPConnection(new URL("http://www.jenner.ac.uk/scripts/"));
		if (proxy)
			con.setCurrentProxy(host, port);

		NVPair[] post = {
				new NVPair("seq", texto),
				new NVPair("dna", ""),
				new NVPair("orf", "3"),
				new NVPair("HLA", "A0101"),
				new NVPair("tcut", "5"),
				new NVPair("output", "0.05"),
				new NVPair("submit", "Submit")
		};

		rsp = con.Get("/../scripts/additive3in1.pl", post);

		String retorno = rsp.getText();
		return retorno;
	}
}
